package cn.tellsea.modifier.utils;

import java.io.*;
import java.util.*;

/**
 * @author Tellsea
 * @date 2022/3/16
 */
public class MainTest {

    /**
     * 删除目录
     */
    private static List<String> deleteFolderList = Arrays.asList(".git", ".idea", "logs", "target");
    /**
     * 删除文件
     */
    private static List<String> deleteFileList = Arrays.asList("LICENSE");
    /**
     * 模块名称修改
     */
    private static Map<String, String> updateFolderMap = new HashMap<String, String>() {
        {
            put("ruoyi-admin", "model-admin");
            put("ruoyi-common", "model-common");
            put("ruoyi-framework", "model-framework");
            put("ruoyi-generator", "model-generator");
            put("ruoyi-quartz", "model-quartz");
            put("ruoyi-system", "model-system");
        }
    };
    /**
     * 排除文件后缀不替换内容
     */
    private static List<String> excludeFileSuffix = Arrays.asList(".jar", ".pdm", ".pdb");

    public static void main(String[] args) {
        // 根目录
        String rootPath = "D:\\Workspace\\TemplateWorkspace\\project-system-test";
        // 服务端
        String systemName = "project-service";
        String systemNameTarget = "hhlkj-exam";
        String projectPath = rootPath + File.separator + systemNameTarget;
        // 组织名称
        String orgName = "hhlkj";

        // 修改项目名
        System.out.println(rootPath + File.separator + systemName);
        System.out.println(projectPath);
        File oldProFile = new File(rootPath + File.separator + systemName);
        if (oldProFile.renameTo(new File(projectPath))) {
            System.out.println("【成功】修改项目名：" + systemName + " -> " + systemNameTarget);
        } else {
            System.out.println("【失败】修改项目名：" + systemName + " -> " + systemNameTarget);
        }

        if (true) {
            return;
        }
        List<String> filePathList = FileUtils.getFilePathList(projectPath);
        for (String filePath : filePathList) {
            // 删除多余文件夹
            for (String folder : deleteFolderList) {
                if (filePath.contains(File.separator + folder)) {
                    String fileName = filePath.substring(0, filePath.indexOf(folder) + folder.length());
                    File file = new File(fileName);
                    if (file.exists()) {
                        System.out.println("【成功】删除多余文件夹：" + filePath);
                        FileUtils.deleteDirectory(fileName);
                    }
                }
            }
            // 删除多余文件
            for (String name : deleteFileList) {
                if (filePath.contains(name)) {
                    System.out.println("【成功】删除多余文件：" + filePath);
                    new File(filePath).delete();
                }
            }
        }

        // 修改模块名
        filePathList = FileUtils.getFilePathList(projectPath);
        for (String filePath : filePathList) {
            File file = new File(filePath);
            if (file.isDirectory()) {
                // 修改模块名
                String fileName = filePath.substring(filePath.lastIndexOf(File.separator) + 1);
                if (updateFolderMap.containsKey(fileName)) {
                    File oldFile = new File(filePath);
                    oldFile.renameTo(new File(filePath.substring(0, filePath.lastIndexOf(File.separator) + 1) + updateFolderMap.get(fileName)));
                    System.out.println("【成功】修改模块名：" + fileName + " -> " + updateFolderMap.get(fileName));
                }
            }
        }

        // 修改包路径
        filePathList = FileUtils.getFilePathList(projectPath);
        for (String filePath : filePathList) {
            File file = new File(filePath);
            if (file.isDirectory()) {
                // 修改包路径
                if (filePath.contains("ruoyi")) {
                    File oldFile = new File(filePath);
                    oldFile.renameTo(new File(filePath.replace("ruoyi", orgName)));
                    System.out.println("【成功】修改包路径：" + filePath + " -> " + filePath.replace("ruoyi", orgName));
                }
            }
        }

        // 修改文件内容
        filePathList = FileUtils.getFilePathList(projectPath);
        for (String filePath : filePathList) {
            File file = new File(filePath);
            if (!file.isDirectory()) {
                String suffix = filePath.substring(filePath.lastIndexOf("."));
                if (!excludeFileSuffix.contains(suffix)) {
                    try {
                        FileInputStream inputStream = new FileInputStream(filePath);
                        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                        String str;
                        while ((str = bufferedReader.readLine()) != null) {
                            if (str.contains("ruoyi")) {
                                FileUtils.updateFileRow(file, "ruoyi", orgName);
                                System.out.println("【成功】修改类路径：" + filePath);
                            }
                        }
                        bufferedReader.close();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }


}
